package demo1;

import java.util.function.Function;
import java.util.function.UnaryOperator;

/**
 * @author lilingzhi
 * @since 2022/12/21 15:18
 */
public class DerivedFunction implements UnaryOperator<Double> {

    private static final double DELTA_X = 0.000_0001;

    private Function<Double,Double> function ;

    public DerivedFunction(UnaryOperator<Double> fun) {
        this.function = fun;
    }

    /**
     * Applies this function to the given argument.
     *
     * @param aDouble the function argument
     * @return the function result
     */
    @Override
    public Double apply(Double aDouble) {
        // 实现原理
        return (function.apply(aDouble + DELTA_X)  - function.apply(aDouble) ) / DELTA_X;
    }
}
